package cn.itsource.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.itsource.api.openfeign.LoginOpenfeign;
import cn.itsource.constants.ErrorCode;
import cn.itsource.constants.RConst;
import cn.itsource.pojo.LoginRo;
import cn.itsource.pojo.domain.Customer;
import cn.itsource.mapper.CustomerMapper;
import cn.itsource.pojo.dto.RegisterDto;
import cn.itsource.pojo.ro.WxChatRegisterRo;
import cn.itsource.result.JSONResult;
import cn.itsource.service.ICustomerService;
import cn.itsource.service.WxChatService;
import cn.itsource.utils.BitStatesUtils;
import cn.itsource.utils.NameUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 客户 服务实现类
 * </p>
 *
 * @author whale
 * @since 2024-07-08
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {

    @Autowired
    private LoginOpenfeign loginOpenfeign;

    @Autowired
    private WxChatService wxChatService;

    @Override
    public JSONResult wxChatRegisterRo(WxChatRegisterRo registerRo) {
        RegisterDto wxChatOpenid = wxChatService.getWxChatOpenid(registerRo.getCode());
        //String userPhone = wxChatService.getUserPhone(registerRo.getPhoneCode());
        String userPhone="18234782385";
        //判断存不存在
        Customer one = super.getOne(new LambdaQueryWrapper<Customer>().eq(Customer::getOpenId, wxChatOpenid.getOpenid()));
        if (one == null) {
            String name = NameUtil.getName();
            initCustomer(wxChatOpenid,userPhone,name);
            loginOpenfeign.createLogin(buildLoginRo(wxChatOpenid,name,userPhone));
            return JSONResult.message("微信绑定成功");
        }

        return JSONResult.error(ErrorCode.WX_CHAT_REPEAT_ERROR);
    }

    private LoginRo buildLoginRo(RegisterDto wxChatOpenid,String name,String userPhone) {
        LoginRo loginRo = new LoginRo();
        loginRo.setPassword("123345");
        loginRo.setType(RConst.Num.INT_2);
        loginRo.setOpenId(wxChatOpenid.getOpenid());
        loginRo.setUsername(name);
        loginRo.setPhone(userPhone);
        loginRo.setAdmin(false);
        return loginRo;
    }

    private void initCustomer(RegisterDto wxChatOpenid, String userPhone,String name) {
        long id = IdUtil.createSnowflake(RConst.Num.INT_1, RConst.Num.INT_1).nextId();
        long state = BitStatesUtils.addState(RConst.Num.INT_0, BitStatesUtils.OP_PHONE);
        long state1 = BitStatesUtils.addState(state, BitStatesUtils.OP_INIT_PAY_PASSWORD);
        Long binaryString = Long.valueOf(Long.toBinaryString(state1));  //转化成二进制
        Customer customer = new Customer().setId(id)
                .setSex(RConst.Num.INT_1)
                .setCreateTime(new Date())
                .setPhone(userPhone)
                .setOpenId(wxChatOpenid.getOpenid())
                .setBitState(binaryString)
                .setName(name);
        super.save(customer);

    }
}
